package com.usooft.metaTalent.db.system.form;

import com.usooft.framework.core.query.Sort;
import com.usooft.framework.web.form.ListForm;
import com.usooft.metaTalent.core.system.entity.SearchTerm;
import com.usooft.metaTalent.core.system.query.SearchTermQuery;

import java.lang.Boolean;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;

import lombok.Setter;

@Setter
public class SearchTermListForm extends ListForm<SearchTermQuery> {
    private Long idIs;

    private Long[] idIn;

    private LocalDateTime createdDateOnOrBefore;

    private LocalDateTime createdDateOnOrAfter;

    private String contentContains;

    private Boolean isHotIs;

    private SearchTerm.PortType portTypeIs;

    private SearchTerm.Position positionIs;

    private Boolean isTopIs;

    private Boolean isDeleteIs;

    @Override
    public SearchTermQuery buildQuery() {
        return SearchTermQuery.builder()
                .filter(buildFilter())
                .sorts(buildSorts())
                .limit(getLimit())
                .build();
    }

    private SearchTermQuery.Filter buildFilter() {
        return SearchTermQuery.Filter.builder()
                .idIs(idIs)
                .idIn(idIn)
                .createdDateOnOrBefore(createdDateOnOrBefore)
                .createdDateOnOrAfter(createdDateOnOrAfter)
                .contentContains(contentContains)
                .isHotIs(isHotIs)
                .portTypeIs(portTypeIs)
                .positionIs(positionIs)
                .isTopIs(isTopIs)
                .isDeleteIs(isDeleteIs)
                .build();
    }

    private SearchTermQuery.Sorts buildSorts() {
        SearchTermQuery.Sorts.Builder builder = SearchTermQuery.Sorts.builder();
        for (String sortItem : getSortItems()) {
            Sort.Order order = getSortOrder(sortItem);
            switch (sortItem) {
                case "-id", "id" -> builder.id(order);
            }
        }
        return builder.build();
    }
}
